﻿(*
    The MIT License

    Copyright (c)2010 Fondevila Ltd

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
*)



let selectSubscription (subs: uk.co.fondevila.GoGo.Reader.Subscription seq) =
    let subArray = Seq.toArray subs
    let lenArray = Array.length subArray

    for i in 0..(lenArray - 1) do
        printfn "[%d]%s" (i + 1) subArray.[i].Title

    printfn "Select a subscription between 1 and %d (0 to exit): " lenArray

    let input = System.Console.ReadLine()
    let success, inputNo = System.Int32.TryParse(input)

    if success && inputNo > 0 && inputNo <= lenArray then
        Some(subArray.[inputNo - 1])
    else None


let selectItem (items: uk.co.fondevila.GoGo.Reader.ReaderItem seq) =
    let itemArray = Seq.toArray items
    let lenArray = Array.length itemArray

    for i in 0..(lenArray - 1) do
        printfn "[%d]%s" (i + 1) itemArray.[i].Title

    printfn "Select an item between 1 and %d (0 to exit): " lenArray

    let input = System.Console.ReadLine()
    let success, inputNo = System.Int32.TryParse(input)

    if success && inputNo > 0 && inputNo <= lenArray then
        Some(itemArray.[inputNo - 1])
    else None
        

let showItem (item: uk.co.fondevila.GoGo.Reader.ReaderItem) =
    item.Summary |> printfn "%s"
    printfn "Print any key to continue."
    System.Console.ReadLine() |> ignore


[<EntryPoint>]
let main args =
    printf "Enter your Google user name or gmail address: "
    let user = System.Console.ReadLine()
    printf "Enter your password: "
    let pwd = System.Console.ReadLine()

    use rdr = new uk.co.fondevila.GoGo.Reader.ReaderService(user, pwd, "uk.co.fondevila.goreader")
    let rec inputEvalLoop() =
        match rdr.getSubscriptions() |> selectSubscription with
        | Some(sub) ->
            match rdr.getItemsFromFeed(sub.Url, 0) |> selectItem with
            | Some(item) -> 
                showItem item
                inputEvalLoop()
            | _ -> ()
        | _ -> ()
    inputEvalLoop()
    0

